<template>
  <a-card :bordered='false'>

    <!-- 查询区域 -->
    <div class='table-page-search-wrapper'>
      <a-form layout='inline' @keyup.enter.native='searchQuery'>
        <a-row :gutter='24'>

          <a-col :md='4' :sm='8'>
            <a-form-item label='商品名称'>
              <j-input placeholder='请输入商品名称' v-model='queryParam.name'></j-input>
            </a-form-item>
          </a-col>
          <a-col :md='4' :sm='8'>
            <a-form-item label='物料编码'>
              <j-input placeholder='请输入物料编码' v-model='queryParam.skuCode'></j-input>

            </a-form-item>
          </a-col>
          <a-col :md='4' :sm='8'>
            <a-form-item label='一级分类'>
              <a-select
                v-model="queryParam.productCategoryIdLevel1"
                style="width: 100%"
                :getPopupContainer="node => node.parentNode"
                placeholder="">
                <a-select-option v-for="(category,categoryIndex) in categoryList" :key="categoryIndex.toString()"
                                 :value="category.id">
                  {{ category.name }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md='4' :sm='8'>
            <a-form-item label='线上销售'>
              <j-dict-select-tag v-model='queryParam.publishStatus' placeholder='请选择线上销售'
                                 dictCode="yn"/>
            </a-form-item>
          </a-col>
          <a-col :md='4' :sm='8'>
            <span style='float: left;overflow: hidden;' class='table-page-search-submitButtons'>
              <a-button type='primary' @click='searchQuery' icon='search'>查询</a-button>
              <a-button @click='searchReset' icon='reload' style='margin-left: 8px'>重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <div>
      <div class='ant-alert ant-alert-info' style='margin-bottom: 16px;'>
        <i class='anticon anticon-info-circle ant-alert-icon'></i> 已选择 <a
        style='font-weight: 600'>{{ selectedRowKeys.length }}</a>项
        <a style='margin-left: 24px' @click='onClearSelected'>清空</a>
      </div>

      <a-table
        ref='table'
        size='middle'
        bordered
        rowKey='id'
        :columns='columns'
        :dataSource='dataSource'
        :pagination='ipagination'
        :loading='loading'
        :rowSelection='{selectedRowKeys: selectedRowKeys, onChange: onSelectChange,type:type}'
        @change='handleTableChange'
        :customRow='clickThenCheck'>

        <span slot='eStatus' slot-scope='text, record'>
          <a-switch size='small' :checked="(text === '1')" @change='handlePublishStatus(record)'/>
        </span>

        <span slot='wStatus' slot-scope='text, record'>
          <a-switch size='small' :checked='(text === 1)'/>
        </span>

        <span slot='action' slot-scope='text, record'>
          <a @click='handleEdit(record)'>编辑</a>
          <a v-has="'product:updatePrice'" style="margin-left: 20px;" @click='handleEditPrice(record)'>修改价格</a>
          <a-divider type='vertical'/>
          <a @click='handlePicDetail(record)'>主图</a>
          <a-divider type='vertical'/>
        </span>

      </a-table>
    </div>
    <!-- table区域-end -->


    <a-tabs>
      <a-tab-pane tab="起订"  :forceRender="true">
        <pmsProductMinimumQuantityList ref="PmsProductMinimumQuantityList"></pmsProductMinimumQuantityList>
      </a-tab-pane>

    </a-tabs>


    <!-- 表单区域 -->
    <pmsProduct-modalJXV2 ref='modalForm' @ok='modalFormOk'></pmsProduct-modalJXV2>
    <select-partner-product-modal ref="productModalForm" @selectFinished="selectOK"/>
    <!-- 状态修改快捷方式 -->
    <pms-product-status-modal ref='productStatusModal' @ok='modalFormOk'></pms-product-status-modal>
    <!-- 主图修改快捷方式 -->
    <pms-product-pic-modal ref='productPicModal' @ok='modalFormOk'></pms-product-pic-modal>

    <pms-product-export-modal ref='productExportModal' @ok='modalFormOk'></pms-product-export-modal>

    <pms-product-recycle-bin-modal :visible.sync='recycleBinVisible' @ok='modalFormOk'/>
    <pmsProductPriceEdit ref="PmsProductPriceEdit" @ok='modalFormOk'></pmsProductPriceEdit>
  </a-card>
</template>

<script>
import PmsProductModalJXV2 from './modules/PmsProductModalJXV2'
import PmsProductStatusModal from './modules/PmsProductStatusModal'
import PmsProductPicModal from './modules/PmsProductPicModal'
import {JeecgListMixin} from '@/mixins/JeecgListMixin'
import {httpAction, deleteAction, postAction, getAction} from '@/api/manage'
import PmsSkuStockModal from './modules/PmsSkuStockModal'
import JInput from '@/components/jeecg/JInput'
import PmsProductRecycleBinModal from './modules/PmsProductRecycleBinModal'
import SelectPartnerProductModal from './modules/SelectPartnerProductModal'
import PmsProductExportModal from '@views/pms/modules/PmsProductExportModal'
import PmsProductPriceEdit from './modules/PmsProductPriceEdit'
import PmsProductMinimumQuantityList from './PmsProductMinimumQuantityList'

export default {
  name: 'PmsProductListJXV2',
  mixins: [JeecgListMixin],
  components: {
    PmsProductModalJXV2,
    PmsProductStatusModal,
    PmsProductPicModal,
    PmsSkuStockModal,
    JInput,
    PmsProductRecycleBinModal,
    SelectPartnerProductModal,
    PmsProductExportModal,
    PmsProductPriceEdit,
    PmsProductMinimumQuantityList
  },
  data() {
    return {
      description: '商品信息管理页面',
      recycleBinVisible: false,
      categoryList: [],
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '品牌',
          align: 'center',
          dataIndex: 'brandName'
        },
        {
          title: '一级分类',
          align: 'center',
          dataIndex: 'productCategoryNameLevel1'
        },
        {
          title: '商品名称',
          align: 'center',
          dataIndex: 'name',
        },
        {
          title: '箱规',
          align: 'center',
          dataIndex: 'boxUnit'
        },
        {
          title: '物料编码',
          align: 'center',
          dataIndex: 'skuCode'
        },
        {
          title: '线上销售',
          align: 'center',
          dataIndex: 'publishStatus',
          scopedSlots: {customRender: 'eStatus'}
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          width: 290,
          scopedSlots: {customRender: 'action'}
        }
      ],
      type: 'radio',
      url: {
        toNetStock: "/pms/pmsProductV2/toNetStock",
        addBatch: "/pms/pmsProductV2/addBatch",
        edit: '/pms/pmsProductV2/edit',
        editProduct: '/pms/pmsProductV2/editProductStatus',
        list: '/pms/pmsProductV2/list',
        delete: '/pms/pmsProductV2/delete',
        deleteBatch: '/pms/pmsProductV2/deleteBatch',
        wxmaSyncBatch: '/pms/pmsProductV2/wxmaSyncBatch',
        wxmaGetProduct: '/pms/pmsProductV2/wxmaGetProduct',
        exportXlsUrl: 'pms/pmsProductV2/exportXls',
        importExcelUrl: 'pms/pmsProductV2/importExcel',
        getProductCategorys: '/pms/pmsProductCategory/queryLevelListAndChild',
        autoUpdateProduct: "/pms/pmsProduct/autoUpdateProduct",
      }
    }
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },
  created() {
    this.getCategoryList()
  },
  methods: {
    getCategoryList() {
      getAction(this.url.getProductCategorys, {}).then(res => {
        if (res.success) {
          this.categoryList = res.result
        } else {
          console.log(res.message)
        }
      })
    },
    clickThenCheck(record) {
      return {
        on: {
          click: () => {
            this.onSelectChange(('' + record.id).split(','), [record])
          }
        }
      }
    },
    handlePicDetail(record) {
      this.$refs.productPicModal.show(record)
    },
    handleDelete: function (id) {
      let that = this
      deleteAction(that.url.delete, {id: id}).then((res) => {
        if (res.success) {
          that.$message.success(res.message)
          that.loadData()
        } else {
          that.$message.warning(res.message)
        }
      })
    },
    searchQuery: function () {
      this.loadData(1)
    },
    handlePublishStatus(record) {
      // return
      let newStatus = ''
      if (record.publishStatus == '1') {
        newStatus = '0'
      } else {
        newStatus = '1'
      }
      let editObj = {}
      editObj.id = record.id
      editObj.publishStatus = newStatus
      this.handleStatusEdit(editObj, record)
    },
    handlePublishStatus2(record) {
      // if (record.storeStatus == 1) {
      //   record.storeStatus = 0
      // } else {
      //   record.storeStatus = 1
      // }
      let newStatus = ''
      if (record.storeStatus == '1') {
        newStatus = 0
      } else {
        newStatus = 1
      }
      let editObj = {}
      editObj.id = record.id
      editObj.storeStatus = newStatus
      this.handleStatusEdit(editObj, record)
    },
    handleStatusEdit(editObj, record) {
      const that = this
      that.confirmLoading = true
      console.log(editObj)
      httpAction(this.url.editProduct, editObj, 'put').then((res) => {
        if (res.success) {
          that.$message.success(res.message)
          if(editObj.publishStatus) {
            record.publishStatus = editObj.publishStatus
          }
          if(editObj.storeStatus) {
            record.storeStatus = editObj.storeStatus
          }
          that.$emit('ok')
        } else {
          that.$message.warning(res.message)
        }
      }).finally(() => {
        that.confirmLoading = false;
      })
    },
    toNetStock() {
      postAction(this.url.toNetStock).then((res) => {
        if (res.success) {
          this.$message.success(res.message);
        } else {
          this.$message.warning(res.message);
        }
      })
    },

    selectOK(data) {
      let ids = [];
      //params.productIdList = [];
      for (let a = 0; a < data.length; a++) {
        ids.push(data[a].id);
      }
      console.log(ids);
      postAction(this.url.addBatch, ids).then((res) => {
        if (res.success) {
          this.$message.success(res.message);
          this.loadData();
        } else {
          this.$message.warning(res.message);
        }
      })
    },
    autoUpdateProduct() {
      getAction(this.url.autoUpdateProduct, null).then((res) => {
        if (res.success) {
          this.$message.success(res.message);
          this.loadData();
        } else {
          this.$message.warning(res.message);
        }
      })
    },
    handleEditPrice(record) {
      this.$refs.PmsProductPriceEdit.edit(record)
    },
    onSelectChange(selectedRowKeys, selectionRows) {
      this.selectedRowKeys = selectedRowKeys;
      this.selectionRows = selectionRows;
      this.$refs.PmsProductMinimumQuantityList.getMain(this.selectedRowKeys[0]);
    },
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>
